﻿Imports TodoMicroSolutionPackV1.productos.dal.dto
Imports TodoMicroSolutionPackV1.dal.dto
Imports NHibernate
Imports TodoMicroSolutionPackV1.dal.dao
Public Class frmABMProductoPrecios
    Private prod As producto
    Private _session As ISession
    Private DAOFactory As NhibernateDAOFactory
    Dim bSrc As New BindingSource

    Protected ReadOnly Property Session As ISession
        Get
            If _session Is Nothing Then
                _session = NhibernateSessionFactory.getSession
            End If
            Return _session
        End Get
    End Property

    Private Sub frmABMProductoPrecios_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If Not _session Is Nothing Then
            _session.Dispose()
        End If
    End Sub
    Private Sub frmABMProductoPrecios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DAOFactory = New NhibernateDAOFactory(Session)
        comunes.populateCombobox(DAOFactory.getConstanteDAO.getConstantes("facturacion", "moneda"), "valor", "id", cmbMoneda)
    End Sub

    Private Sub btnCargarProducto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarProducto.Click
        prod = comunes.getProducto(Session)
        If Not prod Is Nothing Then
            txtId.Text = prod.id
            txtCodigo.Text = prod.codigo
            txtDescripcion.Text = prod.descripcion
            bSrc.DataSource = prod.precios
            grdPrecios.DataSource = bSrc
        End If
    End Sub
    Private Sub btnInsertar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsertar.Click
        Dim nuevoPrecio As New precio
        Using tx As ITransaction = Session.BeginTransaction
            nuevoPrecio.precio_neto = CDbl(txtPrecioNeto.Text)
            nuevoPrecio.moneda = cmbMoneda.SelectedItem
            nuevoPrecio.lista_id = CInt(txtList.Text)
            nuevoPrecio.fecha = Now
            prod.addPrecio(nuevoPrecio)
            DAOFactory.getProductoDAO.SaveOrUpdate(prod)
            tx.Commit()
        End Using
        bSrc.ResetBindings(False)
    End Sub

    Private Sub btnBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBorrar.Click
        Using tx As ITransaction = Session.BeginTransaction
            For Each row In grdPrecios.SelectedRows
                grdPrecios.Rows.Remove(row)
            Next
            tx.Commit()
        End Using
    End Sub

    Private Sub grdPrecios_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdPrecios.CellContentClick

    End Sub

    Private Sub grdPrecios_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdPrecios.CellContentDoubleClick
        Dim precio As precio = prod.precios.Item(grdPrecios.CurrentRow.Index)
        txtPrecioNeto.Text = precio.precio_neto
        txtList.Text = precio.lista_id
        cmbMoneda.SelectedIndex = cmbMoneda.FindStringExact(precio.moneda_nombre)
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        Using tx As ITransaction = Session.BeginTransaction
            Dim precio As precio = prod.precios.Item(grdPrecios.CurrentRow.Index)
            precio.precio_neto = CDbl(txtPrecioNeto.Text)
            precio.lista_id = txtList.Text
            precio.moneda = cmbMoneda.SelectedItem
            bSrc.ResetBindings(False)
            tx.Commit()
        End Using

    End Sub

    Private Sub txtPrecioBruto_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPrecioBruto.LostFocus
        If Not prod Is Nothing Then
            txtPrecioNeto.Text = Math.Round(CDbl(txtPrecioBruto.Text) / (CDbl(prod.iva.valor) / 100 + 1), 2)
        End If
    End Sub

    Private Sub txtPrecioNeto_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPrecioNeto.TextChanged
        If Not prod Is Nothing Then
            txtPrecioBruto.Text = Math.Round(CDbl(txtPrecioNeto.Text) * (CDbl(prod.iva.valor) / 100 + 1), 2)
        End If
    End Sub
End Class